import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('view/Login')
const Home = () => import('view/Home')
const User = () => import('view/User')
const Admin = () => import('view/Admin')
const Stu = () => import('view/Stu')
const Tea = () => import('view/Tea')
const LoginRecord = () => import('view/LoginRecord')
const Topic = () => import('view/Topic')
const Feedback = () => import('view/Feedback')
const TopicChecked = () => import('view/TopicChecked')
const Classes = () => import("view/Classes")
const SelfTopic = () => import("view/SelfTopic")
const UploadPunish = () => import("view/UploadPunish")
const Reference = () => import("view/Reference")
const Export = () => import("view/Export")
const Report = () => import("view/Report")
const NotFound = () => import("view/404")
const Application = () => import("view/Application")
const CheckApplication = () => import("view/CheckApplication")
Vue.use(VueRouter)
const routes = [
    {
        name: "login",
        path: "/login",
        component: Login
    }, {
        name: 'home',
        path: "/home",
        component: Home,
        children: [
            {
                name: "user",
                path: '/user',
                component: User
            },
            {
                name: "stu",
                path: '/stu',
                component: Stu
            },
            {
                name: "tea",
                path: '/tea',
                component: Tea
            },
            {
                name: "admin",
                path: '/admin',
                component: Admin
            },
            {
                name: "loginRecord",
                path: '/loginRecord',
                component: LoginRecord
            },
            {
                name: "topic",
                path: '/topic',
                component: Topic
            },
            {
                name: "feedback",
                path: '/feedback',
                component: Feedback
            },
            {
                name: "topicChecked",
                path: '/topicChecked',
                component: TopicChecked
            },
            {
                name: "classes",
                path: '/classes',
                component: Classes
            },
            {
                name: "selfTopic",
                path: '/selfTopic',
                component: SelfTopic
            },
            {
                name: "uploadPunish",
                path: '/uploadPunish',
                component: UploadPunish
            },
            {
                name: "reference",
                path: '/reference',
                component: Reference
            },
            {
                name: "export",
                path: '/export',
                component: Export
            },
            {
                name: "report",
                path: '/report',
                component: Report
            },
            {
                name: "application",
                path: '/application',
                component: Application
            },
            {
                name: "checkApplication",
                path: '/checkApplication',
                component: CheckApplication
            },
        ]
    },
    {
        name: "404",
        path: '/404',
        component: NotFound
    },
    {
        path: "/",
        redirect: "/login"
    }
]
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
//路由导航守卫
const menus = JSON.parse(sessionStorage.getItem("menus") || '[]')
router.beforeEach((to, from, next) => {
    if (window.sessionStorage.getItem("token") || to.fullPath == "/login") {
        let flag = false;
        if (to.fullPath == "/login" || to.fullPath == "/404" || to.fullPath == "/403" || menus.length == 0) {
            window.sessionStorage.setItem("activePath", to.path)
            return next()
        }
        for (const item of menus) {
            for (const itemChild of item.childMenu) {
                if (`/${to.name}` == itemChild.url) {
                    flag = true;
                    break;
                }
            }
            if (flag) {
                break;
            }
        }
        if (flag) {
            window.sessionStorage.setItem("activePath", to.path)
            return next();
        } else {
            window.sessionStorage.setItem("activePath", '/404')
            return next('/404')
        }
    } else {
        window.sessionStorage.setItem("activePath", "/login")
        return next("/login");
    }
})
export default router